﻿using System;
using System.Data.SqlClient;
using Ntt.NttData.Been.CommonLibrary.Database.SqlServer;

namespace NTTDATA.BEENChina.CaseMonitor.DAO
{
    public class CaseDBAccesser
    {
        private const string INSERTRUNLOG = "INSERT caserunlog SELECT @CaseId,@RunTime";
        private const string GETLASTRUNTIME = "SELECT MAX(RunTime) FROM caserunlog WHERE CaseID=@CaseId";

      
        private static string ConnectionString
        {
            get
            {
                return System.Configuration.ConfigurationManager.AppSettings["connectionstring"];
             } 
            
        }

        public static  void InsertRunLog(string caseId, DateTime datetime)
        {
            if (  string.IsNullOrEmpty(ConnectionString))
            {
                throw new ArgumentException("connection string is not setting!");
            }

            if (string.IsNullOrEmpty(caseId))
            {
                throw new ArgumentException("caseId is not allowed null or empty");
            }

            SqlParameter[] parameters = 
            {
                new SqlParameter("@CaseId", caseId),
                new SqlParameter ("@RunTime",datetime )
            };

            (new SqlHelper()).ExecuteNonQuery(ConnectionString,System.Data.CommandType.Text ,  INSERTRUNLOG, parameters);
        }

        public static DateTime GetLastRunTime(string caseId)
        {
            if (caseId == null && caseId == string.Empty)
                throw new Exception("");

            SqlParameter[] parameters = 
            {
                new SqlParameter("@CaseId", caseId),                
            };

            string maxRunTime = new SqlHelper().ExecuteScalar(ConnectionString, System.Data.CommandType.Text, GETLASTRUNTIME, parameters).ToString();
            //example "2013-7-30 9:49:40"

            if (String.IsNullOrEmpty(maxRunTime))
                return DateTime.MaxValue;
            else
                return DateTime.Parse(maxRunTime);

           /* using (SqlDataReader reader = (new SqlHelper()).ExecuteReader (ConnectionString,System.Data.CommandType.Text ,GETLASTRUNTIME,parameters))
            {
                if(reader.Read())
                {
                    if (String.IsNullOrEmpty(reader[0].ToString()))
                    {
                        return DateTime.MaxValue;
                    }
                    else
                    {
                        DateTime lastRunTime = (DateTime)reader[0];
                        return lastRunTime;
                    }
                    
                }

                reader.Close();
            }

            return DateTime.MaxValue;*/
        }

        public static void InsertProcessLog(string title, string target,string message)
        {
            if (string.IsNullOrEmpty(ConnectionString))
            {
                throw new ArgumentException("connection string is not setting!");
            }

            string insertProcesslogSql = "INSERT INTO  [ProcessLog] " +
                                         " ([Date],[Title],[Target],[Message])VALUES(GETDATE(),@title,@target,@message)";
           

            SqlParameter[] parameters = 
            {
                 new SqlParameter("@title", title),
                new SqlParameter("@target", target),
                new SqlParameter ("@message",message )
            };

            new SqlHelper().ExecuteNonQuery(ConnectionString, System.Data.CommandType.Text, insertProcesslogSql, parameters); 
        }


        public static System.Data.DataSet GetMonitorRecords(string title,DateTime dateFrom,DateTime dateTo)
        {
            if (string.IsNullOrEmpty(ConnectionString))
            {
                throw new ArgumentException("connection string is not setting!");
            }

              string sql = "SELECT   [Target],[Date],[Message] FROM [ProcessLog] WHERE Title = @title  AND [Date] BETWEEN @dateFrom AND  @dateTo order by ID desc ";
    

            SqlParameter[] parameters = 
            {
                 new SqlParameter("@title", title),
                new SqlParameter("@dateFrom", dateFrom),
                new SqlParameter ("@dateTo",dateTo )
            };

            return  new SqlHelper().ExecuteDataset(ConnectionString, System.Data.CommandType.Text, sql, parameters); 
        }
    }
}